<?php

function next_page($list, \Illuminate\Http\Request $request)
{
    $page = (int) $request->page;
    $page = $page ? $page : 1;
    $next_page = 0;
    if ($list->total() > $page) {
        $next_page = $page + 1;
    }
    return $next_page;
}

function to_https_url(string $url)
{
    if (strpos($url, 'http://') === 0) {
        return 'https://' . substr($url, 7);
    }
    return $url;
}

function to_http_url(string $url)
{
    if (strpos($url, 'https://') === 0) {
        return 'http://' . substr($url, 6);
    }
    return $url;
}

function weibo_short_url($longUrl)
{
    $sourceId = \App\Models\SettingsCache::get('SINA_SHORT_URL_SOURCE_ID');
    $url = "https://api.t.sina.com.cn/short_url/shorten.json?source={$sourceId}&url_long={$longUrl}";
    $content = file_get_contents($url);
    $contentArr = json_decode($content);
    if (JSON_ERROR_NONE !== json_last_error()) {
        return '';
    }
    if (is_object($contentArr) && null !== $contentArr->error) {
        \Illuminate\Support\Facades\Log::error('新浪短网址：' . $contentArr->error);
        return '';
    }
    if (!count($contentArr)) {
        return '';
    }
    \Illuminate\Support\Facades\Log::info("短网址：{$contentArr[0]->url_short}，原网址：{$longUrl}");
    return $contentArr[0]->url_short;
}

function wechat_short_url($longUrl)
{
    $url = '';
    try {
        $obj = \App\Models\EasyWechat::getApp()
            ->url->shorten($longUrl);
        $url = $obj->short_url;
    } catch (\Exception $e) {
        $url = $longUrl;
    }
    return $url;
}

/**
 * @param $min
 * @param $max
 */
function percentile_rand($min, $max)
{
    return rand(((int) 100 * $min), ((int) 100 * $max)) / 100;
}

function format_time($second)
{
    $second = (int) $second;
    $hour = str_pad(floor($second / 3600), 2, '0', STR_PAD_LEFT);
    $min = str_pad(floor(($second % 3600) / 60), 2, '0', STR_PAD_LEFT);
    $sec = str_pad(floor($second % 60), 2, '0', STR_PAD_LEFT);
    return "{$hour}:{$min}:{$sec}";
}

function append_uniqid($url, $uniqid)
{
    if (strpos($url, '&uniqid=') > 0 || strpos($url, '?uniqid=') > 0) {
        return $url;
    }
    if (strpos($url, '?') > 0) {
        $url .= '&';
    } else {
        $url .= '?';
    }
    $url .= 'wechat_uniqid=' . $uniqid;
    return $url;
}

function url_params(string $url)
{
    $result = [];
    if (strpos($url, '?') > 0) {
        $arr = explode('?', $url);
        // $str = 'aa=11&bb=22';
        $str = $arr[1];
        $arr = explode('&', $str);
        foreach ($arr as $str) {
            // $str = 'bb=22';
            $item = explode('=', $str);
            $key = urldecode($item[0]);
            $value = isset($item[1]) ? urldecode($item[1]) : '';
            $result[$key] = $value;
        }
    }
    return $result;
}

function fee_rate() : int
{
    return \App\Models\SettingsCache::get('fee_rate', 15);
}

function byte_format($size, $dec = 2){
    $a = array('', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
    $pos = 0;
    while ($size >= 1024) {
        $size /= 1024;
        $pos++;
    }
    return round($size, $dec) . " " . $a[$pos] . 'B';
}